package Pattern.ObserverPattern.Program1;

import java.util.*;

interface AlarmListener {//------------- 1

    public void alarm();
}

abstract class Gate {
    public void action() {
        open();
        close();
    }

    public abstract void open();

    public abstract void close();
}

class CautionLight implements AlarmListener {
    public void alarm() {
        System.out.println("警示灯闪烁！");
    }
}

class Annunciator implements AlarmListener {
    public void alarm() {
        System.out.println("报警器发出警报！");
    }
}

class ExitGate extends Gate implements AlarmListener//------------- 2
{
    public void alarm() {
        action();//------------- 3
    }

    public void open() {
        System.out.println("逃生门开启！");
    }

    public void close() {
        System.out.println("隔热门关闭！");
    }
}

class ThermoSensor {
    private ArrayList alarmListeners = new ArrayList();

    public void register(AlarmListener al) {
        al.alarm();//------------- 4
    }

    public void trigger() {
        for (Object obj : alarmListeners) {
            obj.notify();//------------- 5
        }
    }
}

class Test {
    public static void main(String args[]) {
        AlarmListener light, annunciator, exitGate;
        light = new CautionLight();
        annunciator = new Annunciator();
        exitGate = new ExitGate();

        ThermoSensor sensor;
        sensor = new ThermoSensor();//------------- 6

        sensor.register(light);
        sensor.register(annunciator);
        sensor.register(exitGate);
        sensor.trigger();//------------- 7
    }
}

